package pub.cleangao.codegen;


import io.ous.jtoml.JToml;
import io.ous.jtoml.Toml;
import io.ous.jtoml.TomlTable;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import pub.cleangao.codegen.sqlexe.DbType;

import java.io.File;
import java.io.IOException;


@Data
public class GenConfigHolder {

    private static final String configPath = "code-generate-config.toml";

    /**
     * 数据库类型
     * SqlServer
     * MySQL
     * Maria
     * Oracle
     * Postgres
     * SqlLite
     *
     * @see DbType
     */
    private Integer dbType;

    /**
     * 数据库名称
     */
    private String dbName;

    /**
     * schema(pgsql|oracle)
     */
    private String schemaName;

    /**
     * 代码生成目录
     */
    private String rootPath;

    /**
     * 单体项目
     */
    private boolean singlePot;

    /**
     * 微服务的Provider模块名称
     */
    private String serviceModule;

    /**
     * 微服务的stub模块名称
     */
    private String stubModule;

    // -------------------------------

    /**
     * 数据库连接
     */
    private String jdbcUrl;

    /**
     * 驱动类名
     */
    private String driverClass;

    /**
     * 数据库用户名
     */
    private String username;
    /**
     * 数据库密码
     */
    private String password;

    /**
     * 包含哪些表
     **/
    private String tableInclude;
    /**
     * 排除哪些表
     **/
    private String tableExclude;
    /**
     * 去除表名前缀
     **/
    private String discardPrefix;
    /**
     * 去除表名后缀
     **/
    private String discardSuffix;

    /**
     * 作者
     */
    private String author;


    /**
     * 应用名
     */
    private String applicationName;

    /**
     * 全局代码基本包
     **/
    private String basePackage;

    /**
     * 生成的项目层级
     */
    private String generateFolder;


    public GenConfigHolder(String rootPath) {
        this.rootPath = rootPath;
        try {
            Toml result = JToml.parse(new File(rootPath + File.separator + configPath));
            // ------------------------[application]----------------------------
            TomlTable appTable = result.getTomlTable("application");
            this.author = appTable.getString("author");
            this.applicationName = appTable.getString("name");
            this.basePackage = appTable.getString("basePackage");
            this.generateFolder = appTable.getString("generateFolder");
            this.serviceModule = appTable.getString("serviceModule");
            this.stubModule = appTable.getString("stubModule");
            // --------------------------[database]----------------------------
            TomlTable dbTable = result.getTomlTable("database");
            this.driverClass = dbTable.getString("driverClass");
            this.jdbcUrl = dbTable.getString("jdbcUrl");
            this.username = dbTable.getString("userName");
            this.password = dbTable.getString("password");
            this.schemaName = dbTable.getString("schema");
            // --------------------------[table]----------------------------
            TomlTable tbTable = result.getTomlTable("table");
            this.tableInclude = tbTable.getString("include");
            this.tableExclude = tbTable.getString("exclude");
            this.discardPrefix = tbTable.getString("discardPrefix");
            this.discardSuffix = tbTable.getString("discardSuffix");

            // --------------------------others----------------------------
            DbType dbType = DbType.fromDriverClass(this.driverClass);
            if (dbTable == null) {
                throw new RuntimeException("不支持的数据库类型, 驱动: " + this.driverClass);
            }
            this.dbType = dbType.getType();
            if (dbType.getSignature().equals(DbType.POSTGRE_SQL.getSignature())) {
                if (this.schemaName == null || this.schemaName.trim().equals("")) {
                    this.schemaName = "public";
                }
            }
            // 对 mysql | mariadb 读取dbname
            // jdbc:p6spy:mariadb://172.29.53.183:3306/managend?useUnicode=true...
            if (dbType.getSignature().equals(DbType.MYSQL.getSignature())
                    || dbType.getSignature().equals(DbType.MARIADB.getSignature())
            ) {
                if (StringUtils.isNotBlank(this.schemaName)) {
                    this.dbName = this.schemaName;
                } else {
                    int qasI = this.jdbcUrl.indexOf("?");
                    String url = this.jdbcUrl.substring(0, qasI);
                    int slaI = url.lastIndexOf("/");
                    this.dbName = url.substring(slaI + 1);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        trimAllField(this);
    }

    private void trimAllField(GenConfigHolder that) {
        that.setDbName(that.getDbName() == null ? "" : that.getDbName().trim());
        that.setSchemaName(that.getSchemaName() == null ? "" : that.getSchemaName().trim());
        that.setRootPath(that.getRootPath() == null ? "" : that.getRootPath().trim());
        that.setServiceModule(that.getServiceModule() == null ? "" : that.getServiceModule().trim());
        that.setStubModule(that.getStubModule() == null ? "" : that.getStubModule().trim());
        that.setJdbcUrl(that.getJdbcUrl() == null ? "" : that.getJdbcUrl().trim());
        that.setDriverClass(that.getDriverClass() == null ? "" : that.getDriverClass().trim());
        that.setUsername(that.getUsername() == null ? "" : that.getUsername().trim());
        that.setPassword(that.getPassword() == null ? "" : that.getPassword().trim());
        that.setTableInclude(that.getTableInclude() == null ? "" : that.getTableInclude().trim());
        that.setTableExclude(that.getTableExclude() == null ? "" : that.getTableExclude().trim());
        that.setDiscardPrefix(that.getDiscardPrefix() == null ? "" : that.getDiscardPrefix().trim());
        that.setDiscardSuffix(that.getDiscardSuffix() == null ? "" : that.getDiscardSuffix().trim());
        that.setAuthor(that.getAuthor() == null ? "" : that.getAuthor().trim());
        that.setApplicationName(that.getApplicationName() == null ? "" : that.getApplicationName().trim());
        that.setBasePackage(that.getBasePackage() == null ? "" : that.getBasePackage().trim());
        that.setGenerateFolder(that.getGenerateFolder() == null ? "" : that.getGenerateFolder().trim());
    }

}
